package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.ManufactureManufactureDto;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ManufactureApplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/manufacture/detail")
@Controller
public class ManufactureManufactureController {

    @Autowired
    private ManufactureManufactureService manufactureManufactureService;

    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private ManufactureProcedureService manufactureProcedureService;

    @Autowired
    private ManufactureProcedureModuleService manufactureProcedureModuleService;

    @Autowired
    private ManufactureApplyService manufactureApplyService;

    @Autowired
    private StoragePayService storagePayService;

    @Autowired
    private StoragePayDetailsService storagePayDetailsService;

    /**
     * 1.修改生产计划表派工标志位
     * 2.生产总表添加数据
     * 3.生产工序表添加数据
     * 4.生产工序明细添加数据
     */

    @RequestMapping("addManufactureDetails")
    @ResponseBody
    public boolean addManufactureDetails(@RequestBody ManufactureManufactureDto dto) {
        String typeNumber = numberUtils.getTypeNumber("301");
        dto.setManufactureId(typeNumber);
        boolean flag = manufactureManufactureService.addManufactured(dto);
        return flag;
    }

    //查询生产总表（未审核）
    @RequestMapping("queryManufacture")
    @ResponseBody
    public PageUtils<ManufactureManufacture> queryManufacture(Integer pageNo, Integer pageSize) {
        QueryWrapper<ManufactureManufacture> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG", "S001-0");
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureManufacture> list = manufactureManufactureService.list(wrapper);
        PageInfo<ManufactureManufacture> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureManufacture> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    //根据id查询生产总表
    @RequestMapping("getIdQuery")
    @ResponseBody
    public ManufactureManufacture getIdQuery(Integer rid) {
        ManufactureManufacture manufacture = manufactureManufactureService.getById(rid);
        return manufacture;
    }

    //根据生产总表id查询生产工序
    @RequestMapping("getIdQueryDesign")
    @ResponseBody
    public List<ManufactureProcedure> getIdQueryDesign(Integer pid) {
        ManufactureManufacture manufacture = manufactureManufactureService.getById(pid);
        QueryWrapper<ManufactureProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID", manufacture.getId());
        List<ManufactureProcedure> list = manufactureProcedureService.list(wrapper);
        return list;
    }

    @RequestMapping("getIdQueryDetails")
    @ResponseBody
    public List<ManufactureProcedureModule> getIdQueryDetails(Integer pid) {
        QueryWrapper<ManufactureProcedureModule> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID", pid);
        List<ManufactureProcedureModule> list = manufactureProcedureModuleService.list(wrapper);
        return list;
    }

    //根据工序明细id查询工序名称和工序设计编号
    @RequestMapping("getIdQueryNumber")
    @ResponseBody
    private List getIdQueryNumber(Integer id) {
        List<Object> list = new ArrayList<>();
        ManufactureProcedure service = manufactureProcedureService.getById(id);
        list.add(service.getProcedureName());
        QueryWrapper<ManufactureManufacture> wrapper = new QueryWrapper<>();
        wrapper.eq("id", service.getParentId());
        ManufactureManufacture one = manufactureManufactureService.getOne(wrapper);
        list.add(one.getManufactureId());
        return list;
    }

    //审核修改
    @RequestMapping("manufactureReview")
    @ResponseBody
    public boolean manufactureReview(ManufactureManufacture manufacture) {
        boolean flag;
        //审核不通过
        if (manufacture.getCheckTag().equals("S001-2")) {
            String group = manufacture.getApplyIdGroup();
            if (group.contains("/") == false) {
                QueryWrapper<ManufactureApply> wrapper = new QueryWrapper<>();
                wrapper.eq("APPLY_ID", group);
                ManufactureApply one = manufactureApplyService.getOne(wrapper);
                one.setManufactureTag("P001-0");
                manufactureApplyService.updateById(one);
            } else {
                String[] split = group.split("/");
                for (int i = 0; i < split.length; i++) {
                    QueryWrapper<ManufactureApply> wrapper = new QueryWrapper<>();
                    wrapper.eq("APPLY_ID", split[i]);
                    List<ManufactureApply> list = manufactureApplyService.list(wrapper);
                    for (int j = 0; j < list.size(); j++) {
                        ManufactureApply apply = list.get(j);
                        apply.setManufactureTag("P001-0");
                        manufactureApplyService.updateById(apply);
                    }
                }
            }

            QueryWrapper<ManufactureProcedure> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PARENT_ID", manufacture.getId());
            List<ManufactureProcedure> procedureList = manufactureProcedureService.list(queryWrapper);
            for (int i = 0; i < procedureList.size(); i++) {
                QueryWrapper<ManufactureProcedureModule> moduleQueryWrapper = new QueryWrapper<>();
                moduleQueryWrapper.eq("PARENT_ID", procedureList.get(i).getId());
                List<ManufactureProcedureModule> modules = manufactureProcedureModuleService.list(moduleQueryWrapper);
                for (int j = 0; j < modules.size(); j++) {
                    manufactureProcedureModuleService.removeById(modules.get(j).getId());
                }
                manufactureProcedureService.removeById(procedureList.get(i).getId());
            }
            flag = manufactureManufactureService.removeById(manufacture.getId());
        } else {
            //审核通过
            flag = manufactureManufactureService.updateById(manufacture);
            QueryWrapper<ManufactureProcedure> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PARENT_ID", manufacture.getId());
            List<ManufactureProcedure> list = manufactureProcedureService.list(queryWrapper);
            //生成出库表和出库明细表
            for (int i = 0; i < list.size(); i++) {
                ManufactureProcedure procedure = list.get(i);
                QueryWrapper<ManufactureProcedureModule> procedureModuleQueryWrapper = new QueryWrapper<>();
                procedureModuleQueryWrapper.eq("PARENT_ID", procedure.getId());
                List<ManufactureProcedureModule> modules = manufactureProcedureModuleService.list(procedureModuleQueryWrapper);
                StoragePay pay = new StoragePay();
                String typeNumber = numberUtils.getTypeNumber("402");
                //设置出库单编号
                pay.setPayId(typeNumber);
                //设置出库理由
                pay.setReason("C002-1");
                //设置详细出库理由
                pay.setReasonexact("" + manufacture.getManufactureId() + "/" + procedure.getProcedureName());
                BigDecimal sum = new BigDecimal(0);
                BigDecimal price = new BigDecimal(0);
                for (int j = 0; j < modules.size(); j++) {
                    ManufactureProcedureModule module = modules.get(j);
                    BigDecimal temp = module.getAmount();
                    BigDecimal total = module.getSubtotal();
                    sum = sum.add(temp);
                    price = price.add(total);
                }
                //设置总件数
                pay.setAmountSum(sum);
                //设置总金额
                pay.setCostPriceSum(price);
                //设置登记人
                pay.setRegister(manufacture.getRegister());
                //设置登记时间
                pay.setRegisterTime(manufacture.getRegisterTime());
                //设置审核标志
                pay.setCheckTag("S001-1");
                //设置调度标志
                pay.setStoreTag("K002-1");
                //设置审核人
                pay.setChecker(manufacture.getChecker());
                //设置审核时间
                pay.setCheckTime(manufacture.getCheckTime());
                storagePayService.save(pay);

                //添加出库明细
                for (int z = 0; z < modules.size(); z++) {
                    ManufactureProcedureModule moduleDetails = modules.get(z);
                    StoragePayDetails payDetails = new StoragePayDetails();
                    //设置父级序号
                    payDetails.setParentId(pay.getId());
                    //设置产品编号
                    payDetails.setProductId(moduleDetails.getProductId());
                    //设置产品名称
                    payDetails.setProductName(moduleDetails.getProductName());
                    //设置数量
                    payDetails.setAmount(moduleDetails.getAmount());
                    //设置单价
                    payDetails.setCostPrice(moduleDetails.getCostPrice());
                    //设置小计
                    payDetails.setSubtotal(moduleDetails.getSubtotal());
                    //设置确认出库数量
                    payDetails.setPaidAmount(new BigDecimal(0));
                    //设置出库标志
                    payDetails.setPayTag("K002-1");
                    storagePayDetailsService.save(payDetails);
                }
            }
        }

        return flag;
    }


    @RequestMapping("selectAllManufactured")
    @ResponseBody
    public PageUtils<ManufactureManufacture> selectAllManufactured(ManufactureApplyVo vo, Integer pageNo, Integer pageSize){
        PageUtils<ManufactureManufacture> pageUtils = manufactureManufactureService.getAll(vo, pageNo, pageSize);
        return pageUtils;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public PageUtils<ManufactureManufacture> selectAll(Integer pageNo,Integer pageSize){
        QueryWrapper<ManufactureManufacture> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-1");
        wrapper.eq("MANUFACTURE_PROCEDURE_TAG","S002-0");
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureManufacture> list = manufactureManufactureService.list(wrapper);
        PageInfo<ManufactureManufacture> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureManufacture> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    //根据主键id查询生产总表
    @RequestMapping("getIdQueryDesignManufacture")
    @ResponseBody
    public ManufactureManufacture getIdQueryDesignManufacture(Integer makeId){
        ManufactureManufacture manufacture = manufactureManufactureService.getById(makeId);
        return manufacture;
    }

    //根据主键id查询生产工序名称
    @RequestMapping("getIdQueryManufactureDetails")
    @ResponseBody
    public List<ManufactureProcedure> getIdQueryManufactureDetails(Integer makeId){
        QueryWrapper<ManufactureProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",makeId);
        List<ManufactureProcedure> list = manufactureProcedureService.list(wrapper);
        return list;
    }

    //根据工序id查询生产工序
    @RequestMapping("getId")
    @ResponseBody
    public ManufactureProcedure getId(Integer gxId){
        ManufactureProcedure procedure = manufactureProcedureService.getById(gxId);
        return procedure;
    }
    //根据工序id查询生产工序物料明细
    @RequestMapping("getIdByDetails")
    @ResponseBody
    public List<ManufactureProcedureModule> getIdByDetails(Integer gxId){
        QueryWrapper<ManufactureProcedureModule> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",gxId);
        List<ManufactureProcedureModule> list = manufactureProcedureModuleService.list(wrapper);
        return list;
    }

    @RequestMapping("selAll")
    @ResponseBody
    public PageUtils<ManufactureManufacture> selAll(Integer pageNo,Integer pageSize){
        QueryWrapper<ManufactureManufacture> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-1");
        wrapper.eq("MANUFACTURE_PROCEDURE_TAG","S002-1");
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureManufacture> list = manufactureManufactureService.list(wrapper);
        PageInfo<ManufactureManufacture> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureManufacture> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }
}
